<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="isActive">
      <slot name="item-icon-active"></slot>
    </div>
    <div v-else>
      <slot name="item-icon"></slot>
    </div>
    <div :style="dtcolorFun">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  data() {
    return {
      // isActive: false,
    };
  },
  props: {
    // 默认字体颜色
    path: String,
    dtColor:{
       type: String,
       default: ''
    },
    // 默认字体粗细
    dtFontw:{
       type: String,
       default: ''
    }
  },
  methods: {
    itemClick() {
      this.$router.replace(this.path);  // 不支持缓存
      // this.$router.push(this.path);
     //  console.log("ok");
    },
  },
  computed: {
    isActive() {
      // 判断$route.path跳转路径是否与当前路由路径一致并返回 true
      return this.$route.path.indexOf(this.path) !== -1; //判断当前返回的$route.path
    },
    dtcolorFun() {
      // 动态添加样式类
     return this.isActive? [{color: this.dtColor}, {fontWeight: this.dtFontw}]:{}
    }
  }
}
</script>

<style>
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 49px;
  font-size: 14px;
}
.tab-bar-item img {
  width: 24px;
  height: 24px;
  vertical-align: middle;
  /* 去掉图片的margin-top的默认值 */
  margin-top: 2px;
}
</style>